﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Dynamic;
using System.ComponentModel;
using CCH.Entity;

namespace CCH.DAO
{
    [DataObject(true)]
    public class messageDAO
    {
        private Entities model = new Entities();

        #region 取資料

        /// <summary>
        /// 最新消息前5筆
        /// </summary>
        /// <returns></returns>
        public List<message> get_Top5Data(int sfu_no)
        {
            return (from d in model.message
                    where d.state == "1" && d.sfu_no == sfu_no
                    orderby d.mes_datetime descending
                    select d).Skip(0).Take(5).ToList();
        }

        public IQueryable<message> Get_message_Data()
        {
            return (from d in model.message where d.state == "1" orderby d.cla_no, d.mes_datetime descending select d);
        }
        public IQueryable<message> Get_message_Data(int startRowIndex, int maximumRows)
        {
            return Get_message_Data().Skip(startRowIndex).Take(maximumRows);
        }
        public int Get_message_DataCount()
        {
            return Get_message_Data().Count();
        }

        public IQueryable<message> Get_message_AllData(int cla_no)
        {
            return (from d in model.message where d.state == "1" && d.cla_no == cla_no orderby d.mes_datetime descending select d);
        }
        public IQueryable<message> Get_message_AllData(int cla_no, int startRowIndex, int maximumRows)
        {
            return Get_message_AllData(cla_no).Skip(startRowIndex).Take(maximumRows);
        }

        public IQueryable<message> Get_message_AllData(int sfu_no,int cla_no)
        {
            if (cla_no == 0)
            {
                return (from d in model.message where d.state == "1" && d.sfu_no == sfu_no orderby d.mes_datetime descending select d);
            }
            else
            {
                return (from d in model.message where d.state == "1" && d.sfu_no == sfu_no && d.cla_no == cla_no orderby d.mes_datetime descending select d);
            }
        }

        public IQueryable<message> Get_message_AllData(int sfu_no, int cla_no, int startRowIndex, int maximumRows)
        {
            return Get_message_AllData(sfu_no, cla_no).Skip(startRowIndex).Take(maximumRows);
        }

        //取單筆資料
        public message Get_message(int mes_no)
        {
            return (from d in model.message where d.mes_no == mes_no select d).FirstOrDefault();
        }
        #endregion

        #region 取筆數
        public int Get_message_AllData_Count(int sfu_no, int cla_no)
        {
            return Get_message_AllData(sfu_no, cla_no).Count();
        }

        public int Get_message_AllData_Count(int cla_no)
        {
            return Get_message_AllData(cla_no).Count();
        }

        #endregion

        
        //新增
        public void Add_message(message d)
        {
            model.message.AddObject(d);
            Update();
        }

        //刪除
        public void Delete_message(int mes_no, int peo_uid)
        {
            message data = Get_message(mes_no);
            data.state = "2";
            data.createuid = peo_uid;
            data.createtime = DateTime.Now;
            Update();
        }

        //更新
        public void Update()
        {
            model.SaveChanges();

        }
    }
}
